// $(function () {
//     findById();
// })

function findById() {
    let id = sessionStorage.getItem("productId");
    $.ajax({
        url: '/backImg/product/findById',//對應后台servlet的注解地址
        data: {id: id},//传递给后台的数据
        type: 'get',//和后台方法一直
        dataType: 'json',//后台传递给前台的数据格式
        success: function (data) {//data后台传递给前台的数据
            console.log(data);
            setData(data.model);
        }
    })
}

function setData(data) {
    $("#cuisine option[value=" + data.cuisineId + "]").attr("selected", true);
    $("#productName").val(data.name);
    $("#price").val(data.price);
    $("#normalPrice").val(data.normalPrice);
    $("#imgString").attr("src", data.imgHref);
    $("input[name='isShow'][value=" + data.isShow + "]").attr("checked", true);
    $("input[name='isRecommend'][value=" + data.isRecommend + "]").attr("checked", true);

    // $("#cuisine").find("option[value=" + data.cuisineId + "]").attr("selected", true);
}

function save() {
    let data = new FormData();
    data.append("cuisine", $("#cuisine").val());
    data.append("productName", $("#productName").val());
    data.append("price", $("#price").val());
    data.append("normalPrice", $("#normalPrice").val());
    data.append("imgHref", $("#imgHref")[0].files[0]);
    data.append("isShow", $("input[name='isShow']:checked").val());
    data.append("isRecommend", $("input[name='isRecommend']:checked").val());
    data.append("productId", sessionStorage.getItem("productId"));
    data.append("content", editor.txt.html());
    let result = myImgAjax("/back/product/update", data, "post");
    if (result.result == "success") {
        $(".right").load("/html/back/product/productManage.html");
    } else {
        alert("修改产品失败");
    }

}